/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;
using TypeElement = argot.TypeElement;
using TypeException = argot.TypeException;
using TypeInputStream = argot.TypeInputStream;
using TypeOutputStream = argot.TypeOutputStream;
using TypeReader = argot.TypeReader;
using TypeReaderAuto = argot.TypeReaderAuto;
using TypeWriter = argot.TypeWriter;

namespace argot.meta
{
	
	/*
	* An meta.envelop when called will take any object and attempt to write it out to a
	* temporary buffer.  The buffer will then be written with a size.  Size should be
	* an expression as a reference to a size like u16.
	*/
	public class MetaEnvelop
	:MetaBase, TypeReader, TypeWriter, MetaExpression, MetaDefinition
	{
		override public System.String TypeName
		{
			get
			{
				return TYPENAME;
			}
			
		}
		public const System.String TYPENAME = "meta.envelop";
		
		private MetaExpression _size;
		private MetaExpression _type;
		
		public MetaEnvelop(MetaExpression size, MetaExpression type)
		{
			_size = size;
			_type = type;
		}
		
		public virtual System.Object Read(TypeInputStream inStream, TypeElement element)
		{
			if (element is MetaExpression)
			{
				TypeReader reader = new TypeReaderAuto(this.GetType());
				return reader.Read(inStream, element);
			}
			throw new TypeException("shouldn't get here.");
		}
		
		public virtual void Write(TypeOutputStream outStream, System.Object o, TypeElement element)
		{
			MetaEnvelop ma = (MetaEnvelop) o;
			
			outStream.WriteObject("meta.expression", ma._size);
			outStream.WriteObject("meta.expression", ma._type);
		}
		
		public virtual System.Object DoRead(TypeInputStream inStream)
		{
			Object sizeObject = _size.DoRead( inStream );
			
			uint size = 0;
			
			if (sizeObject is System.Byte)
			{
				size = (uint) ((System.Byte) sizeObject);
			}
			else if (sizeObject is System.UInt16)
			{
				size = (uint) ((System.UInt16) sizeObject);
			}
			else if (sizeObject is System.UInt32)
			{
				size = ((System.UInt32) sizeObject);
			}
			else
			{
				throw new TypeException("meta.envelop not able to use size object");
			}
			
			byte[] buffer = new byte[size];
			inStream.Stream.Read(buffer,0,buffer.Length);
			return buffer;
		}
		
		
		/*
		* This won't ever be called directly.  It must be instantiated by another
		* definition.
		*/
		public virtual void DoWrite(TypeOutputStream outStream, System.Object o)
		{
			System.IO.MemoryStream bout = new System.IO.MemoryStream();
			
			TypeOutputStream tmos = new TypeOutputStream(bout, outStream.TypeMap);
			
			_type.DoWrite(tmos, o);
			tmos.Stream.Close();
			bout.Close();
			
			byte[] b = bout.ToArray();
			
			_size.DoWrite(outStream, (System.Object) b.Length);
			outStream.Stream.Write(b,0,b.Length);
		}
	}
}